<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;

class AdminRole extends Model
{
   /**
    * 对应角色列表
    *
    */
    public function GetRole($adminid){
    	$list = AdminRole::where(['admin_id'=>$adminid,'status'=>1])->column('role_id');
    	
        return  $list;
    }
    /**
     * 通过管理员id获得角色id
     *
     */
    public function GetRoleIdByAdminId($adminid){
    	$id = AdminRole::where(['admin_id'=>$adminid,'status'=>1])->value('role_id');
    	
    	return  $id;
    }
  /**
   * 处理关系
   */
  public function DealRel($id,$ids=array()){
    $list = AdminRole::where('admin_id', $id)->select()->toArray();
    $TempId=[];
    foreach($list as $val){
        if(in_array($val['role_id'],$ids)){
            $val['status']=1;
        }else{
            $val['status']=0;
        }
        AdminRole::update($val);
        if(in_array($val['role_id'],$ids)){
            foreach($ids as $key1=>$val1){
                if($val1==$val['role_id']){
                   unset($ids[$key1]);
                   break;
                }
            }
        }    
    }

    if(!empty($ids)){
       $insert=[];
       foreach($ids as $key=>$val){
         if($val){
            $insert[]=['admin_id'=>$id,'role_id'=>$val,'status'=>1];
         }
       }
       $AdminRole = new AdminRole;
       $AdminRole->saveAll($insert);
    }
  }
}